<?php

namespace App\Http\Controllers\v1;

use App\Model\CardModel;
use App\Model\CommentModel;
use App\Model\FavoriteModel;
use App\Model\FlowModel;
use App\Model\UserFavoriteModel;
use App\Model\UserKeepModel;
use App\Model\UserLikeModel;
use App\Model\UserModel;
use http\Env\Response;
use Illuminate\Http\Request;
use App\Http\Controllers\v1\BaseController;
use Illuminate\Support\Facades\Cache;

class UserController extends BaseController
{
    //
    public function getUserInfo(Request $request){
        $uuid = $request ->get('focus_id');
        if(empty($uuid)){
            return $this->error('缺少参数！');
        }
        $user = UserModel::where('id','=',$uuid)->first();
        if($user){
            return $this->success('获取用户',['nick_name' => $user['nick_name'],'head_img' =>$user['head_img']],200);

        }else{
            return $this->error('查询用户信息失败');
        }
    }

    /**
     * 查询是否关注这个用户
     * @param Request $request
     * @return UserController
     */
    public function getIsFocus(Request $request){
        $token = $request ->header('token');
        $uuid =  Cache::get($token)['uuid'];
        $to_uid = $request ->get('focus_id');//被人的用户id

        if(empty($to_uid)){
          return Response() ->json(['code' => 0,'data' => '缺少参数！','time' => date('Y-m-d H:i:s')]) ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }

        $res  = ( new  UserKeepModel()) ->getIsFocus($uuid,$to_uid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }


    /**
     * 关注用户
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function SaveFocusUser(Request $request){
        $token = $request ->header('token');
        $uuid =  Cache::get($token)['uuid'];
        $focus_id= $request ->get('focus_id');

        if(empty($focus_id)){
            return Response() ->json(['code' => 0,'data' => '缺少参数！','time' => date('Y-m-d H:i:s')]) ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }

        $res  = ( new  UserKeepModel()) ->SaveFocusUser($uuid,$focus_id);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }


    /**
     * 取消关注这个表
     * @param Request $request
     * @return UserController|\Illuminate\Http\JsonResponse
     */
    public function deleteFocusUser(Request $request){
        $token = $request ->header('token');
        $uuid =  Cache::get($token)['uuid'];
        $focus_id= $request ->get('focus_id');

        if(empty($focus_id)){
            return Response() ->json(['code' => 0,'data' => '缺少参数！','time' => date('Y-m-d H:i:s')]) ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }

        $res  = ( new  UserKeepModel()) ->deleteFocusUser($uuid,$focus_id);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }


    /**
     * 帖子点赞
     * @param Request $request
     * @return UserController|\Illuminate\Http\JsonResponse
     */
    public function SaveCardLike(Request $request){
        $token = $request ->header('token');
        $uuid =  Cache::get($token)['uuid'];
        $cid = $request ->get('cid');//帖子id
        $uid = $request ->get('uid');
        $common_id = $request ->get('common_id');
        $head_image = $request ->get('head_image');//用户头像

        if( empty($head_image)){
            return Response() ->json(['code' => 0,'data' => '缺少参数！','time' => date('Y-m-d H:i:s')]) ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }

        $res  = ( new  UserLikeModel()) ->SaveCardLike($uuid,$cid,$head_image,$uid,$common_id);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }

    }


    /**
     * 帖子取消点赞
     * @param Request $request
     * @return UserController|\Illuminate\Http\JsonResponse
     */
    public function DeletedCardLike(Request $request){
        $token = $request ->header('token');
        $uuid =  Cache::get($token)['uuid'];
        $cid = $request ->get('cid');//
        $common_id = $request ->get('common_id');

        $res  = ( new  UserLikeModel()) ->DeletedCardLike($uuid,$cid,$common_id);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }


    /**
     * 帖子点赞数量
     * @param Request $request
     * @return UserController|\Illuminate\Http\JsonResponse
     */
    public function CardLikeNum(Request $request){
        $cid = $request ->get('cid');//帖子id

        if(empty($cid)){
            return $this->error('缺少参数！');
        }

        if(empty($cid)){
            return Response() ->json(['code' => 0,'data' => '缺少参数！','time' => date('Y-m-d H:i:s')]) ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }

        $res  = ( new  UserLikeModel()) ->CardLikeNum($cid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }


    /**
     * 获取帖子点赞用户头像
     * @param Request $request
     * @return UserController|\Illuminate\Http\JsonResponse
     */
    public function CardLikeUserImage(Request $request){
        $cid = $request ->get('cid');//帖子id

        if(empty($cid)){
            return Response() ->json(['code' => 0,'data' => '缺少参数！','time' => date('Y-m-d H:i:s')]) ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }

        $res  = ( new  UserLikeModel()) ->CardLikeUserImage($cid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }


    public function getAllUid(){
        return $this->success('ok',Cache::get());
    }

    //获取用户UID
    public function getUid(Request $request)
    {
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];
        return $this->success('成功', $uuid);
    }
    //获取某个用户的头像
    public function getAvarUrl(Request $request){
        $id = $request->input('id');
        $data = UserModel::where(['id'=>$id])->select('head_img')->first();
        return $this->success('ok',$data);
    }

    //测试
    public function test()
    {
        $uid = 10;
        $testController = new LoginController();
        $data = new Data();
        $data->openid = '3483232AD344732FAFCEF75DDD48E48A';
        $res = (new UserModel())->getToken($data);
        if ($res['code']) {
       //     $token = Common::getStringToken($data->openid);//获取token
            $token='3483232AD344732FAFCEF75DDD48E48A';
            Cache::add('3483232AD344732FAFCEF75DDD48E48A', [
                'uuid' => $res['data']['id'],
                'score' => $res['data']['score']//普通权限
            ], env('TOKEN_TIME'));
            return $this->success('ok', $token);
        }
    }

    /**
     * 添加浏览记录
     * @param Request $request
     * @return UserController
     */
    public function SaveFlow(Request $request){
        $sid = $request ->get('sid');
        $cid = $request ->get('cid');
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];

        $res = (new FlowModel()) ->SaveFlow($uuid,$sid,$cid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 获取浏览记录
     * @param Request $request
     * @return UserController
     */
    public function getFlow(Request $request){

        $cid = $request ->get('cid');

        if(empty($cid)){
            return $this->error('缺少参数！');
        }
        $res = (new FlowModel()) ->getFlow($cid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }

    }

    /**
     * 获取我的页面得关注数量
     * @param Request $request
     * @return UserController
     */
    public function getFocusNum(Request $request){
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];
        $res = (new UserModel()) ->getFocusNum($uuid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 获取我的页面得关注数量
     * @param Request $request
     * @return void
     */
    public function getFansNum(Request $request){
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];
        $res = (new UserModel()) ->getFansNum($uuid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 获取我的页面得超赞数量
     * @param Request $request
     * @return UserController
     */
    public function getLikeNum(Request $request){
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];
        $res = (new UserLikeModel()) ->getLikeNum($uuid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 获取我的页面得关注详细
     * @param Request $request
     * @return UserController
     */
    public function getFocusNumDesc(Request $request){
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];
        $res = (new UserKeepModel()) ->getFocusNumDesc($uuid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 获取我的页面得粉丝详细
     * @param Request $request
     * @return UserController
     */
    public function getFansNumDesc(Request $request){
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];
        $res = (new UserKeepModel()) ->getFansNumDesc($uuid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 查询别人的关注
     * @param Request $request
     * @return UserController
     */
    public function getFocusNumTo(Request $request){
        $uid = $request ->get('uid');
        if(empty($uid)){
            return $this->error('缺少参数！');
        }
        $res = (new UserModel()) ->getFocusNum($uid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 查询别人的粉丝
     * @param Request $request
     * @return UserController
     */
    public function getFansNumTo(Request $request){
        $uid = $request ->get('uid');
        if(empty($uid)){
            return $this->error('缺少参数！');
        }
        $res = (new UserModel()) ->getFansNum($uid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 查询别人点赞
     * @param Request $request
     * @return UserController
     */
    public function getLikeNumTo(Request $request){
        $uid = $request ->get('uid');
        if(empty($uid)){
            return $this->error('缺少参数！');
        }
        $res = (new UserLikeModel()) ->getLikeNum($uid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 查询自己的发帖 评论 点赞 收藏（别人）
     * @param Request $request
     * @return UserController
     */
    public function getMyDescAllOrToDate(Request $request){
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];//必填
        $toid = $request ->get('uid'); //可选
        $type = $request ->get('type');//必填

        if(is_null($type)){
            return $this->error('缺少参数！');
        }

        switch ($type){
            case 0://帖子
                $res = (new CardModel()) ->getMyPost($uuid,$toid);
               return $this ->rest($res);
                break;
            case 1://评论
                $res = (new CommentModel()) ->getComment($uuid,$toid);
                return $this ->rest($res);
                break;
            case 2://点赞
                $res = (new UserLikeModel()) ->getLisk($uuid,$toid);
                return $this ->rest($res);
                break;
            case 3://收藏
                $res = (new UserFavoriteModel()) ->getFans($uuid,$toid);
                return $this ->rest($res);
                break;
            default:
                return $this -> error('没有这个选项');
        }

    }

    public function rest($res){
        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }

    }


    /**
     * 收藏
     * @param Request $request
     * @return UserController
     */
    public function SaveCollect(Request $request){
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];//必填
        $cid = $request ->get('cid');
        if(empty($cid)){
            return $this->error('缺少参数');
        }
        $res = (new UserLikeModel()) ->SaveCollect($uuid,$cid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }

    }

    /**
     *删除收藏
     * @param Request $request
     * @return UserController
     */
    public function DeletedCollect(Request $request){
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];//必填
        $cid = $request ->get('cid');
        if(empty($cid)){
            return $this->error('缺少参数');
        }
        $res = (new UserLikeModel()) ->DeletedCollect($uuid,$cid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     *  是否收藏
     * @param Request $request
     * @return UserController
     */
    public function IsFocusCollect(Request $request){
        $token = $request ->header('token');
        $uuid  = Cache::get($token)['uuid'];//必填
        $cid = $request ->get('cid');
        if(empty($cid)){
            return $this->error('缺少参数');
        }
        $res = (new FavoriteModel()) ->IsFocusCollect($uuid,$cid);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }
}


